package com.lcy.accumulation.export.setting;

import java.util.List;

/**
 * <p>创建时间：2017-5-23 10:24</p>
 *
 * @author 娄存银
 * @version 1.0
 */
public class ExportBody extends ExportItem {
    private List<ExportColumn> columns;
    private ColumnHandler handler;
    private Integer maxRow;

    private ExportBody(Integer row, Integer column,Integer maxRow, List<ExportColumn> columns){
        this.row = row;
        this.column = column;
        this.maxRow = maxRow;
        this.columns = columns;
    }

    public void init(){
        if(columns != null){
            Integer column = getColumn();
            Integer row = getRow();
            if(row == null){
                row = 0;
            }
            if(column == null){
                column = 0;
            }
            for (ExportColumn exportColumn:columns) {
                if(exportColumn != null){
                    if(exportColumn.getColumn() == null){
                        exportColumn.setColumn(column);
                    }else {
                        column = exportColumn.getColumn();
                    }
                }
                column++;
            }
        }
    }

    public List<ExportColumn> getColumns() {
        return columns;
    }

    public void setColumns(List<ExportColumn> columns) {
        this.columns = columns;
    }

    public ColumnHandler getHandler() {
        return handler;
    }

    public void setHandler(ColumnHandler handler) {
        this.handler = handler;
    }


    public Integer getMaxRow() {
        return maxRow;
    }

    public void setMaxRow(Integer maxRow) {
        this.maxRow = maxRow;
    }

    public static ExportBody getInstance(Integer row, Integer column,Integer maxRow, List<ExportColumn> columns){
        if(columns == null || columns.size() == 0){
            return null;
        }
        if (row == null){
            row = 0;
        }
        if(column == null){
            column = 0;
        }
        ExportBody body = new ExportBody(row,column,maxRow,columns);
        body.init();
        return body;
    }

    public void initHandler(String handler){
        if(handler == null || handler.isEmpty()){
            return;
        }
        try {
            Class handlerClass = Class.forName(handler);
            this.handler = (ColumnHandler) handlerClass.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }
}
